<?php
class Graduates
{
	const DBTable = "tbl_graduates";

	public $Graduate_id;	public $Title;	public $Description;	public $Status;	public $Is_deleted;	public $Createdate;	public $Updatedate;
	private $__mysql;

	public function __construct()
	{
		$this->Graduate_id = 0;		$this->Title = null;		$this->Description = null;		$this->Status = 0;		$this->Is_deleted = 0;		$this->Createdate = date("Y-m-d H:i:s");		$this->Updatedate = date("Y-m-d H:i:s");
		$this->__mysql = new MYSQL;
	}

	public function Save()
	{
		$sqlStr = "insert into ".self::DBTable."
				(title, description, status, createdate, updatedate)
				values
				(
				'".mysql_real_escape_string($this->Title)."', 				'".mysql_real_escape_string($this->Description)."', 				'".$this->Status."', 				'".$this->Createdate."', 				'".$this->Updatedate."'
				)";
			$this->__mysql->Exec_query($sqlStr);
			$this->Graduate_id = $this->__mysql->Get_insert_id();
			return $this->Graduate_id;
	}

	public function Update()
	{
		$sqlStr = "update ".self::DBTable." set
			title = '".mysql_real_escape_string($this->Title)."', 			description = '".mysql_real_escape_string($this->Description)."', 			status = '".$this->Status."', 			updatedate = '".$this->Updatedate."'
			where graduate_id = '".mysql_real_escape_string($this->Graduate_id)."'";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function Quick_update($column = null, $value = null)
	{
		$sqlStr = "update ".self::DBTable." set
			".$column." = '".mysql_real_escape_string($value)."',
			 updatedate = '".$this->Updatedate."' 
			where graduate_id = '".mysql_real_escape_string($this->Graduate_id)."'";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function List_all($limit = null, $offset = null)
	{
		$sqlStr = "select *
			from ".self::DBTable."
			where is_deleted = 0 
			order by title";
		if(isset($limit) && isset($offset)){
			$sqlStr .= " limit $offset, $limit";
		}
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}

	public function List_all_selectlist()
	{
		$sqlStr = "select graduate_id, title
			from ".self::DBTable."
			where is_deleted = 0 
			order by title";
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}

	public function Delete($graduate_id = 0)
	{
		$sqlStr = "update ".self::DBTable." set is_deleted = 1 where graduate_id = '".mysql_real_escape_string($graduate_id)."'";
			$this->__mysql->Exec_query($sqlStr);

		$sqlStr = "optimize table ".self::DBTable."";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function Select($graduate_id = 0)
	{
		$sqlStr = "select * from ".self::DBTable." where graduate_id = '".mysql_real_escape_string($graduate_id)."'";
		$recordset = $this->__mysql->Exec_query($sqlStr);

		if($this->__mysql->Get_num_rows($recordset) > 0)
		{
			$result = $this->__mysql->Get_data_assoc($recordset);
				$this->Graduate_id = $result['graduate_id'];				$this->Title = $result['title'];				$this->Description = $result['description'];				$this->Status = $result['status'];				$this->Is_deleted = $result['is_deleted'];				$this->Createdate = $result['createdate'];				$this->Updatedate = $result['updatedate'];
			return true;
		}
		else
		{
			return false;
		}
	}

	public function Select_by($fieldname = null, $value = null)
	{
		if(!is_null($fieldname))
		{
			$sqlStr = "select * from ".self::DBTable." where ".$fieldname." = '".mysql_real_escape_string($value)."'";
			$recordset = $this->__mysql->Exec_query($sqlStr);

			if($this->__mysql->Get_num_rows($recordset) > 0)
			{
				$result = $this->__mysql->Get_data_assoc($recordset);
				$this->Graduate_id = $result['graduate_id'];				$this->Title = $result['title'];				$this->Description = $result['description'];				$this->Status = $result['status'];				$this->Is_deleted = $result['is_deleted'];				$this->Createdate = $result['createdate'];				$this->Updatedate = $result['updatedate'];
				return true;
			}
			else
			{
				return false;
			}
		}
	}
}
?>